package com.bzkj.web.controller.ordermanagement.Inbound;

import com.alibaba.fastjson2.JSON;
import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.DocConfirmDTO;
import com.bzkj.document.domain.otherinorder.dto.ICGeneralInH;
import com.bzkj.document.domain.otherinorder.dto.ICGeneralInRequest;
import com.bzkj.document.domain.otherinorder.dto.OtherInboundQueryDTO;
import com.bzkj.document.service.otherinboundorder.OtherInboundOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.web.controller.ordermanagement.Inbound
 * @className: OtherInboundOrderController
 * @description: 其它入库单据控制层
 * @author: C1309
 * @createDate: 2024年11月26日
 * @version: v1.0
 */
@RestController
@RequestMapping("/other-inbound")
public class OtherInboundOrderController extends BaseController {

    /**
     * 其它入库单据业务层
     */
    @Resource
    private OtherInboundOrderService otherInboundOrderService;

    /**
     * 获取NCC其他入库单
     *
     * @return 查询结果
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:list')")
    public TableDataInfo selectNccOtherInboundOrder(@RequestParam Map<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        if (nccRequestDTO.getFbillflag() == null || nccRequestDTO.getFbillflag().isEmpty()) {
            nccRequestDTO.setFbillflag("2");
        }
        return otherInboundOrderService.selectNccOtherInboundOrder(nccRequestDTO);
    }

    /**
     * 获取本地其他入库单据
     *
     * @param otherInboundQueryDTO 查询条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('order:otherinbound:list')")
    public TableDataInfo getLocalOtherInboundOrder(OtherInboundQueryDTO otherInboundQueryDTO) {
        if (otherInboundQueryDTO == null) {
            otherInboundQueryDTO = new OtherInboundQueryDTO();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return otherInboundOrderService.getLocalOtherInboundOrder(otherInboundQueryDTO, pageNum, pageSize);
    }

    /**
     * 新增 NCC 其他入库单据
     *
     * @param responseBodyStr 请求参数
     * @return 新增结果
     */
    @PostMapping("/add-other-order")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:add')")
    @LogB(title = "其他入库单", businessType = BusinessType.INSERT)
    public AjaxResult addOtherInboundOrder(@RequestBody String responseBodyStr) {
        ICGeneralInRequest request = JSON.parseObject(responseBodyStr, ICGeneralInRequest.class);
        request.setLoginUser(getLoginUser());
        // 将库管员字段映射到业务员上
        ICGeneralInH icGeneralInH = request.getIcGeneralInH();
        icGeneralInH.setcBizId(icGeneralInH.getcWhsManagerId());
        return otherInboundOrderService.addOtherInboundOrder(request);
    }

    /**
     * 确认其他入库单据
     *
     * @param docConfirmDTO 确认单据
     * @return 确认结果
     */
    @PostMapping("/confirm")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:confirm')")
    @LogB(title = "其他入库单", businessType = BusinessType.CONFIRM)
    public AjaxResult confirmOtherInboundOrder(@RequestBody DocConfirmDTO docConfirmDTO) {
        docConfirmDTO.setLoginUser(getLoginUser());
        return otherInboundOrderService.confirmOtherInboundOrder(docConfirmDTO);
    }

    /**
     * 其他入库单签字
     *
     * @param cgeneralhid NCC单据表头主键
     * @return 签字结果
     */
    @PutMapping("/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:audit')")
    @LogB(title = "其他入库单", businessType = BusinessType.SIGN)
    public AjaxResult otherInboundOrderSignature(@PathVariable String cgeneralhid) {
        return otherInboundOrderService.otherInboundOrderSignature(cgeneralhid, getLoginUser());
    }

    /**
     * 更新其他入库单据
     *
     * @param responseBody 需更新的数据
     * @return 更新结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:edit')")
    @LogB(title = "其他入库单", businessType = BusinessType.UPDATE, pkFieldNames = "cgeneralhid",
            tableNames = "wms_warehouse_in_other",
            detailTable = "wms_warehouse_in_detail_other"
    )
    public AjaxResult updateOtherInboundOrder(@RequestBody HashMap<String, Object> responseBody) {
        return otherInboundOrderService.updateOtherInboundOrder(responseBody, getLoginUser(), false);
    }


    /**
     * 删除其他入库单据
     *
     * @param header NCC单据表头主键
     * @param reason 删除原因
     * @return 删除结果
     */
    @DeleteMapping("/{header}")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:delete')")
    @LogB(title = "其他入库单", businessType = BusinessType.INVALID)
    public AjaxResult otherInboundOrderDelete(@PathVariable String header,
                                              @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return otherInboundOrderService.deleteOtherInboundOrder(header, getLoginUser(), reason);
    }

    /**
     * 其他入库单据回退
     *
     * @param headerId 需要打回的单据类型
     * @return 回退结果
     */
    @PostMapping("/back/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:otherinbound:edit')")
    @LogB(title = "其他入库单", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("headerid") String headerId) {
        return otherInboundOrderService.orderBack(headerId, getLoginUser());
    }

    /**
     * 失败库存处理
     *
     * @param headerId 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{headerid}")
    @LogB(title = "其他入库单", businessType = BusinessType.INVENTORY)
    @PreAuthorize("@ss.hasPermi('order:otherinbound:audit')")
    public AjaxResult updateInventory(@PathVariable("headerid") String headerId) {
        return otherInboundOrderService.updateInventory(headerId, getLoginUser());
    }
}
